package us.icitap.commandcenter.ui.supervisor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.listeners.SearchListener;
import us.icitap.commandcenter.ui.custom.CustButton;
import us.icitap.commandcenter.ui.custom.CustVerticalLayout;
import us.icitap.commandcenter.utils.HandleNotification;
import us.icitap.commandcenter.utils.Utils;
import us.icitap.utils.Result;

import com.vaadin.server.BrowserWindowOpener;
import com.vaadin.server.FileDownloader;
import com.vaadin.server.StreamResource;
import com.vaadin.server.StreamResource.StreamSource;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class CallSearchLayout extends CustVerticalLayout implements SearchListener{

	
	private final SearchLayout searchLayout = new SearchLayout(this);
	private final ResultLayout resultLayout = new ResultLayout();
	private final CustButton backButton = new CustButton("Kthehu tek kerkimi");
	private final CustButton exportToExcel = new CustButton("Exporto ne Excel");
	private final CustButton printCall = new CustButton("Printo");
	
	
	private  List<Call> searchedCallList ;
	
	public CallSearchLayout() {
		this.setMargin(true);
		this.setSpacing(true);
		this.addStyleName("borders");
		
		this.showSearchLayout();
		this.resultLayout.addMenu(backButton);
		backButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				showSearchLayout();
				
			}
		});
		
		this.resultLayout.addMenu(this.exportToExcel);
		
		this.resultLayout.addMenu(this.printCall);
		
		this.printCall.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				Call c = resultLayout.getSelectedCall();
				if(c!= null){
					showPrintingWindow(c);
				}
				else
					HandleNotification.showWarningOnScreen("Nuk eshte zgjedhur asnje telefonate nga tabela");
				
			}
		});
		
	}
	
	
	private void showSearchLayout(){
		this.removeAllComponents();
		this.addComponent(searchLayout);
		this.setWidth("500px");
	}
	
	private void showResultLayout(){
		Result<StreamSource> result = CommandCenterUI.appStatelessBean.generateExcelFromList(searchedCallList);
		if(result.isSucceeded()){
			StreamResource sr = new StreamResource(result.getObject(), "Lista-e-telefonatave.xls");
			FileDownloader fl = new FileDownloader(sr);
			fl.extend(exportToExcel);
		}
		else
			HandleNotification.showErrorOnScreen(result.getMessage());
		
		this.removeAllComponents();
		this.addComponent(resultLayout);
		this.setSizeUndefined();
	}


	private void storeCallList(List<Call> callList){
		if(searchedCallList != null){
			searchedCallList.clear();
			searchedCallList = null;
		}
		this.searchedCallList = callList;
	}
	
	@Override
	public <T> void searchFinished(T result) {
		Result<List<Call>> res = (Result<List<Call>>) result;
		if(res.isSucceeded()){
			this.storeCallList(res.getObject());
			this.resultLayout.getTable().addCallList(res.getObject());
			showResultLayout();
		}
		else
			HandleNotification.showErrorOnScreen(res.getMessage());
			
	}
	
	
	private void showPrintingWindow(Call call){
		try {
			Result<StreamSource> result = CommandCenterUI.appStatelessBean.generateCallPdf(call, CommandCenterUI.getUser().getUsername());
			if(result.isSucceeded()){
				StreamResource sr = new StreamResource(result.getObject(), "INFO_TEL"+ Utils.formatedDateTime1(new Date()) +".pdf");
				sr.setCacheTime(-1);
				Embedded em = new Embedded(null, sr);
				em.setMimeType("application/pdf");
				
				em.setType(Embedded.TYPE_BROWSER);
				em.setSizeFull(); 
				Window w = new Window("Info telefonate");
			   	w.setHeight("850px");
			   	w.setWidth("1000px");
			   	w.setModal(true);
			   	w.setDraggable(false);
			   	w.setResizable(false);


			   	VerticalLayout vl = new VerticalLayout();
			    vl.setSpacing(true);    
			    vl.setSizeFull();
			    vl.addComponent(em); 
			    w.setContent(vl);
			   CommandCenterUI.getCurrent().addWindow(w);	
			}
			else
				HandleNotification.showErrorOnScreen(result.getMessage());
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
}
